<template>
  <div class="app-container">
    <!--<el-form :inline="true" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input placeholder="请输入名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary">查询</el-button>
        <el-button type="primary">重置</el-button>
      </el-form-item>
    </el-form>-->
    <div class="btn-container">
      <el-button type="primary" @click="openAddCategory">添加类别</el-button>
    </div>
    <el-table
      :height="tableHeight"
      header-cell-class-name="aa"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        align="center"
        type="index"
        width="50"
      />
      <el-table-column
        prop="categoryName"
        align="center"
        label="名称"
      />
      <el-table-column
        prop="categoryName"
        align="center"
        label="图片"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 200px; height: 50px"
            :src="scope.row.categoryIcon"
            fit="fit"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="categoryType"
        align="center"
        label="分类编号"
      />
      <el-table-column
        prop="createTime"
        align="center"
        label="创建时间"
      />
      <el-table-column
        prop="updateTime"
        align="center"
        label="更新时间"
      />
      <el-table-column
        fixed="right"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openUpdateCategory(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="removeCategory(scope.row.categoryId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            :action="upLoadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="categoryName" style="width: 200px" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分类编号" :label-width="formLabelWidth">
          <el-input v-model="categoryType" style="width: 200px" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="isUpdate" type="primary" @click="updateCategory">确 定</el-button>
        <el-button v-else type="primary" @click="addCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryCategory, removeCategory, updateCategory, addCategory } from '@/api/category'
export default {
  name: 'Index',
  data() {
    return {
      upLoadUrl: process.env.VUE_APP_BASE_API + 'product-info/upload',
      categoryId: '',
      categoryType: null,
      formLabelWidth: '100px',
      isUpdate: false,
      title: '',
      imageUrl: '',
      categoryName: '',
      form: {
        region: ''
      },
      value: '',
      tableHeight: window.innerHeight - 170,
      tableData: [],
      loading: false,
      name: '',
      dialogFormVisible: false
    }
  },
  mounted() {
    this.queryCategory()
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!')
      // }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    openUpdateCategory(row) {
      this.dialogFormVisible = true
      this.isUpdate = true
      this.categoryName = row.categoryName
      this.imageUrl = row.categoryIcon
      this.categoryId = row.categoryId
      this.categoryType = row.categoryType
    },
    updateCategory() {
      const data = {
        categoryName: this.categoryName,
        categoryIcon: this.imageUrl,
        categoryId: this.categoryId,
        categoryType: this.categoryType
      }
      updateCategory(data).then(response => {
        this.dialogFormVisible = false
        this.queryCategory()
      })
    },
    removeCategory(categoryId) {
      this.$confirm('确定删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeCategory(categoryId).then(response => {
          this.queryCategory()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    queryCategory() {
      queryCategory().then(response => {
        this.tableData = response.data
      })
    },
    openAddCategory() {
      this.dialogFormVisible = true
      this.isUpdate = false
      this.categoryName = ''
      this.imageUrl = ''
      this.categoryType = null
    },
    addCategory() {
      const data = {
        categoryName: this.categoryName,
        categoryIcon: this.imageUrl,
        categoryType: this.categoryType
      }
      addCategory(data).then(response => {
        this.dialogFormVisible = false
        this.queryCategory()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .btn-container{
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    margin-bottom: 10px;
  }
  .el-icon-plus .avatar-uploader-icon {
    border: 1px dashed #d9d9d9!important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader i{
    border: 1px dashed #d9d9d9!important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
